const jwt = require('jsonwebtoken')
async function auth(ctx,next){
    const {authorization} = ctx.request.header;
    if(authorization) {
        const token = authorization.split(' ')[1]
        console.log(token)
        try{
            ctx.user = jwt.verify(token,'1812a')
            console.log(ctx.user)
            await next()
        }catch(err){
            ctx.status = 401;
            ctx.body = {
                code:-1,
                message:"权限校验失败"+err.message
            }
        }
    } else {
        ctx.status = 401;
        ctx.body = {
            code:-1,
            message:"你没有权限"
        }
    }
}
module.exports = auth;